package org.uoss.core.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public final class DateTimeUtil {
	public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
	public static final String DATA_TIME_FORMAT_YYYY_MM_DD_HH = "yyyy-MM-dd HH:mm";
	public static final String DATA_TIME_FORMAT_YYYY_MM_DD_HH_SS = "yyyy-MM-dd HH:mm:ss";
	
	/**
	 * Format data object to string.
	 * 
	 * @param date
	 * @param formatPattern
	 * @return
	 */
	public static String format(Date date,String formatPattern){
		if(ValidationUtil.isEmpty(formatPattern)){
			formatPattern = DateTimeUtil.DATA_TIME_FORMAT_YYYY_MM_DD_HH_SS;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(formatPattern);
		return sdf.format(date);
	}
	
	/**
	 * Parse date time string to date object.
	 * 
	 * @param dateTime
	 * @return
	 * @throws ParseException
	 */
	public static Date parse(String dateTime) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat(DateTimeUtil.DATA_TIME_FORMAT_YYYY_MM_DD_HH_SS);
		return sdf.parse(dateTime);
	}
}
